package shaman.storage.impl.file.date;

import java.util.Calendar;

public final class DateFormatElement {

    public final int field;
    public final int minWidth;
    public final int shift;
    public final String preString;

    public DateFormatElement(int field, int minWidth) {
        this.field = field;
        this.minWidth = minWidth;
        this.preString = "";
        shift=0;
    }

    public DateFormatElement(int field, int minWidth, String preString) {
        this.field = field;
        this.minWidth = minWidth;
        if (preString == null) {
            this.preString = "";
        } else {
            this.preString = preString;
        }
        shift=0;
    }

    public DateFormatElement(int field, int minWidth, int shift) {
        this.field = field;
        this.minWidth = minWidth;
        this.preString = "";
        this.shift=shift;
    }

    public DateFormatElement(int field, int minWidth, String preString, int shift) {
        this.field = field;
        this.minWidth = minWidth;
        if (preString == null) {
            this.preString = "";
        } else {
            this.preString = preString;
        }
        this.shift=shift;
    }

    public void format(Calendar date, StringBuffer toAppendTo) {
        int value = date.get(field)+shift;
        String fieldValue = String.valueOf(value);
        int addZeros = minWidth - fieldValue.length();
        int i;
        toAppendTo.append(preString);
        for (i = 0; i < addZeros; i++) {
            toAppendTo.append("0");
        }
        toAppendTo.append(fieldValue);
    }
}
